package main

import (
	"fmt"
	"sort"
)

func threenum(nums []int) [][]int {
	res := [][]int{}
	counter := map[int]int{}
	//map 计数去重
	for _, value := range nums {
		counter[value]++
	}

	uniNum := []int{}
	for key := range counter {
		uniNum = append(uniNum, key)
	}
	sort.Ints(uniNum)

	for i := 0; i < len(uniNum); i++ {
		if (uniNum[i]*3 == 0) && counter[uniNum[i]] >= 3 {
			res = append(res, []int{uniNum[i], uniNum[i], uniNum[i]})
		}
		for j := i + 1; j < len(uniNum); j++ {
			if (uniNum[i]*2+uniNum[j] == 0) && counter[uniNum[i]] > 1 {
				res = append(res, []int{uniNum[i], uniNum[i], uniNum[j]})
			}
			if (uniNum[j]*2+uniNum[i] == 0) && counter[uniNum[j]] > 1 {
				res = append(res, []int{uniNum[i], uniNum[j], uniNum[j]})
			}
			c := 0 - uniNum[i] - uniNum[j]
			if c > uniNum[j] && counter[c] > 0 {
				res = append(res, []int{uniNum[i], uniNum[j], c})
			}
		}

	}
	return res
}

func main() {
	array := []int{-1, 0, 1, 2, -1, -4}
	result := threenum(array)
	fmt.Println(result)

}
